
const start = wx.createInnerAudioContext();
const mid = wx.createInnerAudioContext();
const stop = wx.createInnerAudioContext();
 
Component({
/*    options: {
      multipleSlots: true // 在组件定义时的选项中启用多slot支持
   }, */
 
   /**
    * 组件的属性列表
    * 用于组件自定义设置   组件的对外属性
    */
   properties: {
      myPropert: {    // 属性名        myProperty2: String, 简化的定义方式
         type: String, // 类型（必填），目前接受的类型包括：String, Number, Boolean, Object, Array, null（表示任意类型）
         value: '',    // 属性默认 初始值（可选），如果未指定则会根据类型选择一个
        /*  observer: function (newVal, oldVal, changedPath) {
            // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'
            // 通常 newVal 就是新设置的数据， oldVal 是旧数据
         } */
      },
 
      probability: {
         type: Boolean, // 概率开关，默认false 随机
         value: false
      },
 
      fastJuedin: {
         type: Boolean, // 快速转动转盘的开关，默认false
         value: false
      },
 
      repeat: {
         type: Boolean, // 重复抽取开关，默认false
         value: false
      },
 
      size: {
         type: Number, 
         value: 600
      },
 
      zhuanpanArr: { 
         type: Array,
         value: [
            {
               id: 0,
               option: '转盘的标题名称',
               awards: [
                  {
                     id: 0,
                     name: "黄焖鸡", 
                     color: '#549e39',        
                     probability: 10      
                  },
                  {
                     id: 1,
                     name: "拌面扁肉", 
                     color: '#195F00',
                     probability: 10
                  },
                  {
                     id: 2,
                     name: "鸡公煲", 
                     color: '#549e39',
                     probability: 10
                  },
                  {
                     id: 3,
                     name: "烤鱼", 
                     color: '#195F00',
                     probability: 10
                  },
                  {
                     id: 4,
                     name: "寿司", 
                     color: '#549e39',
                     probability: 10
                  },
                  {
                     id: 5,
                     name: "烤肉", 
                     color: '#195F00',
                     probability: 10
                  },
                  {
                     id: 6,
                     name: "火锅",
                     color: '#549e39',
                     probability: 10
                  },
                  {
                     id: 7,
                     name: "家常菜", 
                     color: '#195F00',
                     probability: 10
                  }
                  
               ]
            }
         ]
      },
   },
 
   /**
    * 私有数据,组件的初始数据
    * 可用于模版渲染   
    */
   data: {
      animationData: {}, 
      zhuanflg: false,   
      fastTime: 7600,    
      slowTime: 3900,   
      runDegs: 0,        
      timer: null,       
      block1: 'block', 
      block2: 'none',
      block3: 'none',
      block4: 'none',
      zhuanpanindex:[],
      range:0,
      isShow:'none'
   },
 
   created: function () { },
 
   attached: function () {
      this.setData({
         awardsConfig: this.data.zhuanpanArr[0]
      })
      this.initAdards();
   },
 

   methods: {
     
      isNull(str) {
         if (str == null || str == undefined || str == '') {
            return true;
         } else {
            return false;
         }
      },
 
      
      initAdards() {
         var that = this, awardsConfig = that.data.awardsConfig;
         var t = awardsConfig.awards.length;  // 选项长度
         var e = 1 / t, i = 360 / t, r = i - 90;
 
         for (var g = 0; g < t; g++) {
            awardsConfig.awards[g].item2Deg = g * i + 90 - i / 2 + "deg";//当前下标 * 360/长度 + 90 - 360/长度/2
            awardsConfig.awards[g].afterDeg = r + "deg";
            awardsConfig.awards[g].opacity = '1';
         }
 
         that.setData({
            turnNum: e, // 页面的单位是turn
            awardsConfig: awardsConfig,
         })
 
         that._change();//向父组件传出当前转盘的初始数据
      },
 
     
      reset() {
         var that = this, awardsConfig = that.data.awardsConfig;
         var animation = wx.createAnimation({
            duration: 1,
            timingFunction: "ease"
         });
         that.animation = animation;
         animation.rotate(0).step(), that.data.runDegs = 0;
            that.setData({
            animationData: animation.export(),
            block3: 'none',
            block4: 'block'
         })
         for (let x in awardsConfig.awards) {
            awardsConfig.awards[x].opacity = '1';
         }
         setTimeout(function () {
            that.setData({
               block1: 'block',
               block4: 'none',
               awardsConfig: awardsConfig,
            })
         }, 300)
      },
     
      switchZhuanpan(data, flag) {
         this.setData({
            awardsConfig: data,
            block1: 'block',
            block1: 'none',
            block3: 'none',
            block4: 'none',
            zhuanflg: false,
         })
         this.initAdards();
 
         if (flag) {
            this.reset();
            clearTimeout(this.data.timer);
            start.stop();
            mid.stop();
            stop.stop();
            wx.removeStorageSync('repeatArr');
         }
      },
 
 
 
     
 
      
      _zhuan() {
         var that = this, awardsConfig = that.data.awardsConfig, runDegs = that.data.runDegs;
        
         var r = Math.random() * awardsConfig.awards.length >>> 0, runNum = 8;
        
        
         this.zhuanpanindex=this.properties.zhuanpanArr
         this.range=r
         this.zhuanpanindex.forEach(element => {
            console.log("element.awards[r].name",element.awards[r].name);
            this.setData({
               isShow:''
            })
            
         });



         
         if (that.data.repeat) {
            r = that._queryRepeat(r);
         } else {
            wx.removeStorageSync('repeatArr');
 
            console.log('是否开启了概率？？？', that.data.probability);
            
            if (that.data.probability) {
               r = that._openProbability();
            }
         }
         
 
         
         console.log('当前答案选项的下标=====', r);
         
        
    
         setTimeout(function () {

            runDegs = runDegs || 0, runDegs = runDegs + (360 - runDegs % 360) + (2160 - r * (360 / awardsConfig.awards.length));
 
            var animation = wx.createAnimation({
               duration: that.data.fastJuedin ? that.data.slowTime : that.data.fastTime,
               timingFunction: "ease"
            });
            that.animation = animation;
 

            animation.rotate(runDegs).step(), 0 == r && (runDegs = 0);
 
            that.setData({
               animationData: animation.export(),
               block1: 'none',
               block2: 'block',
               zhuanflg: true,
            })
 
            that._setatZhuan(true);
         }, 100);
 
         that.setData({
            timer: setTimeout(function () {
              
               for (let x in awardsConfig.awards) {
                  if (x != r) {
                     awardsConfig.awards[x].opacity = '0.3';
                  } else {
                     awardsConfig.awards[x].opacity = '1';
                  }
               }
 
               that.setData({
                  animationData: {},
                  s_awards: awardsConfig.awards[r].name,
                  awardsConfig: awardsConfig,
                  block2: 'none',
                  block3: 'block',
                  zhuanflg: false,
               })
               that._myAwards();
               that._setatZhuan(false);
            }, that.data.fastJuedin ? that.data.slowTime : that.data.fastTime)
         })
      },
      clickMe(){
         this.zhuanpanindex.forEach(element => {
            console.log("element.awards[r].name",element.awards[this.range].name);
         });
         var cainame=this.data.awardsConfig.awards[this.range].name
         console.log(cainame);
         wx.request({
            url: 'https://www.daizhongdai.xyz:3007/like/query?name='+cainame,
            success:res=>{
               var resObj=res.data.data;
               console.log(resObj)
              wx.navigateTo({
               url: '/pages/sousuo/sousuo?name='+cainame,
              })
              }
          })
         
      },
 
     
      _openProbability() {
         var that = this, awards = that.data.awardsConfig.awards, arr = [];
         //5, 5, 20, 10 ,30 ,30, 0
         for (let i in awards) {
            if (awards[i].probability != 0) {
               for (var x = 0; x < awards[i].probability; x++) {
                  //把当前的概率数字 以当前选项下标的形式 都添加都空数组中，然后随机这个数组
                  arr.push(i);
               }
            }
         }
         var s = Math.floor(Math.random() * arr.length);
         return arr[s];
      },
 
      
      _queryRepeat(r) {
         var that = this, flag = true, repeatArr = wx.getStorageSync('repeatArr'), repeatArr2 = [], awardsConfig = that.data.awardsConfig;
         if (that.isNull(repeatArr)) {
            repeatArr2.push(r), wx.setStorageSync('repeatArr', repeatArr2);
            return r;
         } else {
            var len = awardsConfig.awards.length, r = Math.random() * len >>> 0;
            for (let i in repeatArr) {
               if (r == repeatArr[i]) {
                  flag = false;
                  if (repeatArr.length == len) {
                     wx.removeStorageSync('repeatArr');
                     repeatArr2.push(r), wx.setStorageSync('repeatArr', repeatArr2);
                     return r;
                  } else {
                     return that._queryRepeat();//递归调用
                  }
               }
            }
            if (flag) {
               repeatArr.push(r), wx.setStorageSync('repeatArr', repeatArr);
               return r;
            }
         }
      },
 
    
      _change() {
         this.triggerEvent('myData', this.data.awardsConfig);
      },
 
      
      _myAwards() {
         this.triggerEvent('myAwards', this.data.s_awards)
      },
 
      
      _setatZhuan(e) {
         this.triggerEvent('startZhuan', e);
      },
 
   }
})
